package pers.cj.pdos.controller;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.IndustryType;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.entity.BusinessSet;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.Industry;
import pers.cj.pdos.service.IBusinessSetService;
import pers.cj.pdos.service.IHouseWaitInService;
import pers.cj.pdos.service.IHouseWaitOutService;
import pers.cj.pdos.service.IIndustryService;
import pers.cj.pdos.utils.DataUtil;
import pers.cj.pdos.utils.PdosUtils;

/**
 * <p>
 * 包含基本信息设置、常用设置、税率设置、移动支付设置（微信和支付宝） 前端控制器
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Controller
@RequestMapping("/sett")
public class BusinessSetController extends BaseController {
    @Autowired
    private IBusinessSetService businessSetService;
    @Autowired
    IHouseWaitInService houseWaitInService;
    @Autowired
    IHouseWaitOutService houseWaitOutService;

    @RequiresPermissions("businessSet:insert")
    @RequestMapping("/businessSet")
    public String businessSet(){
        return "sett/businessSet";
    }

    @ResponseBody
    @RequiresPermissions("businessSet:insert")
    @RequestMapping("/businessSet/update")
    public ResponseEntity update(@RequestBody BusinessSet businessSet){
        if (!PdosUtils.houseInOut(businessSet)) {
            //关闭出入库状态，需要检查是否还有待入库单
            if (houseWaitInService.checkWaitIn()){
                return ResponseEntity.fail("系统中有待入库单，禁止关闭");
            }

            //关闭出入库状态，需要检查是否还有待入库单
            if (houseWaitOutService.checkWait()){
                return ResponseEntity.fail("系统中有待出库单，禁止关闭");
            }
        }



        DataUtil.processEntity(false,getCompany(), businessSet);
        businessSetService.update(businessSet);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequestMapping("/businessSet/getByCompany")
    public ResponseData getByCompany(){
        BusinessSet businessSet = businessSetService.getByCompany(getCompany().getId());
        return ResponseData.success(businessSet);
    }
}
